﻿using System.Collections.Generic;
using CTEM.Models;
using CTEM.Controllers.DAO;
using System;
using CTEM.Controllers.Global;

namespace CTEM.Controllers.BUS
{
    class WarehouseBUS
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly WH_WarehouseDAO _warehouseDAO = new WH_WarehouseDAO();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="warehouseDTO"></param>
        /// <param name="errorMessage"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool ValidateWarehouse(WH_WarehouseDTO warehouse, out string errorMessage, out string fieldName)
        {
            if (warehouse.WarehouseName.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Tên Kho";
                fieldName = "WarehouseName";
                return false;
            }
            else if (warehouse.WarehouseName.Length > 125)
            {
                errorMessage = "Tên Kho Không Được Dài Quá 125 Ký Tự";
                fieldName = "WarehouseName";
                return false;
            }
            else if (warehouse.ManagerName.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Tên Người Quản Lý";
                fieldName = "ManagerName";
                return false;
            }
            else if (warehouse.ManagerName.Length > 50)
            {
                errorMessage = "Tên Người Quản Lý Không Được Dài Quá 50 Ký Tự";
                fieldName = "ManagerName";
                return false;
            }
            else
            {
                errorMessage = Constants.EMPTY_TEXT;
                fieldName = Constants.EMPTY_TEXT;
                return true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public long CreateWarehouse(WH_WarehouseDTO warehouse)
        {
            return _warehouseDAO.CreateWarehouse(warehouse);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public bool UpdateWarehouse(WH_WarehouseDTO warehouse)
        {
            warehouse.UpdatedBy = Global.Global.CurrentUser.Username;
            warehouse.LastUpdated = DateTime.Today;
            return _warehouseDAO.UpdateWarehouse(warehouse);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="warehouseID"></param>
        /// <returns></returns>
        public bool DeleteWarehouse(long warehouseID)
        {
            return _warehouseDAO.DeleteWarehouse(warehouseID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isactive"></param>
        /// <returns></returns>
        public List<WH_WarehouseDTO> LoadWarehouse(bool isactive)
        {
            return _warehouseDAO.LoadAllWarehouses(isactive);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="warehouseID"></param>
        /// <returns></returns>
        public WH_WarehouseDTO GetWarehouseByID(long warehouseID)
        {
            return _warehouseDAO.GetWarehouseByID(warehouseID);
        }
    }
}
